import java.io.FileInputStream;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class JdbcTest {
    public static void main(String[] args) {
        FileInputStream fis=null;
        Connection connection=null;
        try {
            //1.加载配置文件
            Properties pros=new Properties();//先创建一个配置文件对象
            fis=new FileInputStream("jdbc3.properties");//利用输入流读取配置文件信息
            pros.load(fis);//将这个本地配置文件信息，读取到创建的配置文件对象pros中去

            //2.读取配置信息
            String user = pros.getProperty("user");//获取配置文件中user的值
            String password = pros.getProperty("password");//获取配置文件中password的值
            String url = pros.getProperty("url");//获取配置文件中url的值
            String driverClass = pros.getProperty("driverClass");//获取配置文件中driverClass的值

            //打印结果
//            System.out.println("user:"+user);
//            System.out.println("password:"+password);
//            System.out.println("url:"+url);
//            System.out.println("driverClass:"+driverClass);

            //3.加载驱动
            Class.forName(driverClass);
            //4.获取数据库连接
            connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);//打印连接
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection !=null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (fis !=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
